<?php
namespace app\admin\service;

use function think\where;

class SettleAccountsService
{
    public function index($filter)
    {
        $order = model('payee_order');
        
        $where = [
        ];        
        $join = [
            ['__TB_VIP_INFO__ info','a.pay_no = info.vip_no'],
            ['__MEMBER__ member','member.id = info.isid'],
            ['__ORDER__ o','o.order_id = a.order_id'],
        ];
         
        $field = 'a.*,member.real_name,o.pos_order';
        
        if(!empty($filter['act_type']) and $filter['act_type']!=0){
            $where['a.act_type'] = $filter['act_type'];
        }


        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤收款人消费数据代码
        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.payee_no'] = $keyword;
        }
        $orders   = $order->where($where)->join($join)->alias('a')->field($field)->order('id','DESC')->paginate(15,false,['query'=>request()->param() ]);
        $orderSum = $order->where($where)->join($join)->alias('a')->sum('a.score'); 
        $refundSum  = 0;
        //抛开退款部分为正常消费部份
        if(isset($where['a.act_type'])&&($where['a.act_type']==1)){
            $where['a.act_type'] = 2 ;
            $refundSum = $order->where($where)->join($join)->alias('a')->sum('a.score');
        }
        $result['orders']   = $orders;
        $result['orderSum'] = $orderSum - $refundSum;
        return $result;    
    }
	
	public function exportExcel($filter)
    {
        $order = model('payee_order');
    
        $where = [
        ];
        $join = [
            ['__TB_VIP_INFO__ info','a.pay_no = info.vip_no'],
            ['__MEMBER__ member','member.id = info.isid'],
            ['__ORDER__ o','o.order_id = a.order_id'],
        ];
        // 
        $field = 'member.real_name,a.order_id,o.pos_order,a.pay_no,a.payee_no,a.score,a.act_type,o.cashier,FROM_UNIXTIME(a.create_time) as create_time';
    
        if(!empty($filter['act_type']) and $filter['act_type']!=0){
            $where['a.act_type'] = $filter['act_type'];
        }
    
    
        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤收款人消费数据代码
        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.payee_no'] = $keyword;
        }
        $orders   = $order->where($where)->join($join)->alias('a')->field($field)->order('a.id','DESC')->select();
        return $orders;
    }
}

